package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.NewsContent;
import cn.wolfcode.trip.base.domain.NewsPage;
import cn.wolfcode.trip.base.domain.StrategyContent;
import cn.wolfcode.trip.base.domain.StrategyDetail;
import cn.wolfcode.trip.base.query.StrategyDetailQueryObject;
import cn.wolfcode.trip.base.service.INewsPageService;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 攻略文章
 */
@Controller
@RequestMapping("/newsPage")
public class NewsPageController {

    @Autowired
    private INewsPageService newsPageService;


    /**
     * 展示攻略文章
     * @param qo
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") StrategyDetailQueryObject qo, Model model){
        qo.setOrderBy("releaseTime DESC");
        model.addAttribute("pageInfo",newsPageService.query(qo));
        return "newsPage/list";
    }

    /**
     * 获取日报文章内容
     * @param id
     * @return
     */
    @RequestMapping("/getContentById")
    @ResponseBody
    public NewsContent getContentById(Long id){
        return newsPageService.getContentById(id);
    }

    /**
     * 日报的新增与保存
     * @param newsPage
     * @return
     */
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(NewsPage newsPage) {
        JsonResult jsonResult = new JsonResult();

        try {
            if (newsPage.getId() == null) {
                newsPageService.save(newsPage);
            }else{
                newsPageService.update(newsPage);
            }
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.mark("操作异常");
        }
        return jsonResult;
    }


}
